package com.dzwww.recommend.rule.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzwww.recommend.rule.common.api.ApiResult;
import com.dzwww.recommend.rule.common.bo.PageQuery;
import com.dzwww.recommend.rule.common.po.Rule;
import com.dzwww.recommend.rule.service.common.RuleService;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.ws.rs.core.MediaType;
import java.util.Map;

/**
 *
 * <p>
 *  规则管理api接口
 * </p>
 */
@RestSchema(schemaId = "remmberMeDingRule")
@RequestMapping(path = "/api/rule/management", produces = MediaType.APPLICATION_JSON)
public class RuleManageMentController {

    @Resource
    RuleService ruleService;

    /**
     * 添加规则
     */
    @PostMapping
    public ApiResult managementSave(@RequestBody Rule rule){
        boolean add = ruleService.add(rule);
        if(add){
            return ApiResult.succeed("添加成功");
        }
        return ApiResult.fail("","添加失败");
    }

    /**
     * 修改规则管理
     */
    @PutMapping
    public ApiResult managementUpdate(@RequestBody Rule rule){
        boolean b = ruleService.updateById(rule);
        if(b){
            return ApiResult.succeed("修改成功");
        }
        return ApiResult.fail("","修改失败");
    }

    /**
     * 分页查询
     * @return      返回分页参数
     */
    @GetMapping("")
    public ApiResult managementPage(Integer current,Integer size,@RequestParam(value = "name",required = false) String name){
        if(current < 0){
            return ApiResult.fail("","起始页不能小于零");
        }
        Page<Rule> rulePage = new Page<>();
        rulePage.setCurrent(current);
        rulePage.setSize(size);
        Page<Rule> page = ruleService.listPage(rulePage, name);
        return ApiResult.succeed(page);
    }

    /**
     * 删除规则管理
     * @param id 删除的主键ID
     * @return 返回success
     */
    @DeleteMapping("/{id}")
    public ApiResult managementDelete(@PathVariable("id") Long id){
        boolean b = ruleService.delById(id);
        if (b){
            return ApiResult.succeed("删除成功");
        }
        return ApiResult.fail("","删除失败");
    }

    /**
     * 启用禁用
     * @param rule  修改参数
     * @return  返回success
     */
    @PatchMapping
    public ApiResult managementPatch(@RequestBody Rule rule){
        boolean b = ruleService.updateById(rule);
        if(b){
            return ApiResult.succeed("修改成功");
        }
        return ApiResult.fail("","操作失败");
    }

    /**
     * 查询单个规则详情
     * @param id 唯一主键ID
     * @return  返回success
     */
    @GetMapping("/{id}")
    public ApiResult managementGetById(@PathVariable("id")Long id){
        Rule rule = ruleService.getById(id);
        return ApiResult.succeed(rule);
    }
}
